package com.zzdn.mqtool.controller;

import com.zzdn.mqtool.entity.MQConfig;
import com.zzdn.mqtool.service.MQService;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Controller
@RequestMapping("/mq")
public class MQController {
    private static Logger logger = LoggerFactory.getLogger(MQController.class);
    @Autowired
    private MQService mqService;

    @RequestMapping("/sendMq")
    @ResponseBody
    public Map<String,Object> doSendXml(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String xml = request.getParameter("xmlContent").trim();
        int priority = Integer.parseInt(request.getParameter("priority"));
        String messageId = request.getParameter("messageId");
        String correlationId = request.getParameter("correlationId");
      //  logger.info(xml);
        String xmlContent = new String(xml.getBytes("GBK"),"GBK");
       // logger.info(xmlContent);
        return mqService.doSendXml(xmlContent,priority,messageId,correlationId,request);
    }

    @RequestMapping("/getxml")
    @ResponseBody
    public Map<String,Object> getXmlTemplate(@RequestBody String tradeNo){
        JSONArray json = new JSONArray("["+tradeNo+"]");
        String tradeNum = json.getJSONObject(0).get("tradeNo").toString();
        logger.info( tradeNum );
        return mqService.getXmlTemplate(tradeNum);
    }


    /**
     * 修改参数
     * @param config
     * @return
     */
    @RequestMapping("/save_mq_param")
    @ResponseBody
    public  Map<String,Object> saveMqParam(@RequestBody MQConfig config,HttpServletRequest request){
        logger.info(config.toString());
        return mqService.saveMqConfig(config, request);
    }

    @RequestMapping("/getdefault")
    @ResponseBody
    public  Map<String,Object> getDefault(){
        return mqService.getDefault();
    }


    @RequestMapping("/getuserconfig")
    @ResponseBody
    public  Map<String,Object> getUserConfig(HttpServletRequest request){
        return mqService.getUserConfig(request);
    }


}
